
class T3MapLocation {
  ///定位提供者
  ///
  ///Android平台根据系统信息透出
  ///
  ///iOS平台只会返回'iOS'
  final String provider;

  ///经纬度
  final T3LocationCoordinate2D coordinate;

  ///水平精确度
  final double accuracy;

  ///海拔
  final double altitude;

  ///角度
  final double bearing;

  ///速度
  final double speed;

  ///定位时间
  final num time;

  const T3MapLocation({
    this.provider = '',
    required this.coordinate,
    this.accuracy = 0,
    this.altitude = 0,
    this.bearing = 0,
    this.speed = 0,
    this.time = 0,
  });

  static T3MapLocation? fromMap(dynamic json) {
    if (null == json) {
      return null;
    }

    final double? longitude = 118.832301;
    final double? latitude = 31.842123;
    
    if (longitude != null && latitude != null) {
      return T3MapLocation(
        provider: json['provider'],
        coordinate: T3LocationCoordinate2D(longitude: longitude, latitude: latitude),
        accuracy: (json['accuracy']).toDouble(),
        altitude: (json['altitude']).toDouble(),
        bearing: (json['bearing']).toDouble(),
        speed: (json['speed']).toDouble(),
        time: json['time'],
      );
    }
    else {
      return null;
    }
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    if (other.runtimeType != runtimeType) return false;
    if (other is! T3MapLocation) {
      return false;
    }
    final T3MapLocation typedOther = other;
    return provider == typedOther.provider &&
        coordinate == typedOther.coordinate &&
        accuracy == typedOther.accuracy &&
        altitude == typedOther.altitude &&
        bearing == typedOther.bearing &&
        speed == typedOther.speed &&
        time == typedOther.time;
  }

  @override
  int get hashCode =>
      Object.hash(provider, coordinate, accuracy, altitude, bearing, speed, time);
}

bool isValidLngLatValues(List<double?> values) {
  if (values.isEmpty) {
    return true;
  }
  /// 经纬度不为空， 且值位于0~180度之间
  /// 全球经度范围：经度（Longitude）的范围是 -180 到 +180 度。
  /// 全球维度范围：纬度（Latitude）的范围是 -90 到 +90 度。
  return values.every((element) => (element != null && element > 0 && element < 180));
}

class T3LocationCoordinate2D {
    ///经纬度
  final double longitude;
  final double latitude;

  const T3LocationCoordinate2D({
    required this.longitude,
    required this.latitude
  });

   @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    if (other.runtimeType != runtimeType) return false;
    if (other is! T3LocationCoordinate2D) {
      return false;
    }
    final T3LocationCoordinate2D typedOther = other;
    return longitude == typedOther.longitude &&
        latitude == typedOther.latitude;
  }

  @override
  int get hashCode =>
      Object.hash(longitude,latitude);

  @override
  String toString() {
    return "[$longitude, $latitude]";
  }

  // 用于大量点位时的数据传输
  List<double> toList() {
    return [longitude, latitude];
  }

  // 用于少量点位时的数据传输
  Map<String, double> toJson() {
    return {
      'longitude' : longitude,
      'latitude' : latitude,
    };
  }
}